// 本文件不能放在cdn中进行共享，因为需要被webpack转换
function loadJs(url, callback) {
	var script = document.createElement('script');
	script.type = 'text/javascript';
	if (typeof callback != 'undefined') {
		if (script.readyState) {
			script.onreadystatechange = function() {
				if (script.readyState == 'loaded' || script.readyState == 'complete') {
					script.onreadystatechange = null;
					cache[url] = 1;
					callback();
				}
			};
		} else {
			script.onload = function() {
				cache[url] = 1;
				callback();
			};
		}
	}
	script.src = url;
	document.body.appendChild(script);
}
function loadCss(url, callback) {
	var script = document.createElement('link');
	script.rel = 'stylesheet';
	if (typeof callback != 'undefined') {
		if (script.readyState) {
			script.onreadystatechange = function() {
				if (script.readyState == 'loaded' || script.readyState == 'complete') {
					script.onreadystatechange = null;
					cacheCss[url] = 1;
					callback();
				}
			};
		} else {
			script.onload = function() {
				cacheCss[url] = 1;
				callback();
			};
		}
	}
	script.href = url;
	document.body.appendChild(script);
}
let cache = {};
let cacheCss = {};
function loadJsAsync(url) {
	return new Promise((resolve, reject) => {
		if (cache[url]) return resolve();
		loadJs(url, function() {
			resolve();
		});
	});
}
function loadCssAsync(url) {
	return new Promise((resolve, reject) => {
		if (cacheCss[url]) return resolve();
		loadCss(url, function() {
			resolve();
		});
	});
}

function loadComponent(scopeModule, prop = 'default', forceUrl) {
	let [scope, ...module] = scopeModule.split('/');

	module = ['.'].concat(module).join('/');
	return async () => {
		let url = forceUrl || `/${scope}/remoteEntry.js`;
		await loadJsAsync(url);
		// Initializes the share scope. This fills it with known provided modules from this build and all remotes
		await __webpack_init_sharing__('default');

		const container = window[scope]; // or get the container somewhere else
		// Initialize the container, it may provide shared modules
		await container.init(__webpack_share_scopes__.default);
		const factory = await window[scope].get(module);
		const Module = factory();
		// console.log(Module);
		let s = Module[prop];
		if (s instanceof Function) return s();
		return s;
	};
}
window.loadComponent = loadComponent;
window.loadJsAsync = loadJsAsync;
window.loadCss = loadCss;
window.loadCssAsync = loadCssAsync;
